package com.neusoft.yuejian26.yuejian_26.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.neusoft.yuejian26.yuejian_26.Bean.Result;
import com.neusoft.yuejian26.yuejian_26.R;
import com.neusoft.yuejian26.yuejian_26.model.FeedBackModel;
import com.neusoft.yuejian26.yuejian_26.network.RetrofitCallback;

import retrofit2.Call;

public class FeedBackActivity extends AppCompatActivity {

    public EditText fb_content,fb_contact;
    public ImageView svg_back;
    public Button sub_fb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        initview();
        initEvent();
    }

    private class subFB implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            String fbcontent = fb_content.getText().toString();
            String contact = fb_contact.getText().toString().trim();
            if(fbcontent.equals("")){
                Toast.makeText(FeedBackActivity.this,"请填写反馈内容",Toast.LENGTH_SHORT).show();
            }
            else if(contact.equals("")){
                Toast.makeText(FeedBackActivity.this,"请填写联系方式",Toast.LENGTH_SHORT).show();
            }
            else{
                FeedBackModel feedBackModel = new FeedBackModel();
                Call call = feedBackModel.subFb(fbcontent,contact);
                feedBackModel.bindCallback(call, new RetrofitCallback() {
                    @Override
                    public void onResponse(Object o) {
                        Result result = (Result) o;
                        int state = result.getCode();
                        if(1 == state){
                            Toast.makeText(FeedBackActivity.this,"提交成功",Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(FeedBackActivity.this,MainActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putInt("page",4);
                            intent.putExtras(bundle);
                            FeedBackActivity.this.startActivity(intent);
                            finish();
                        }else {
                            Toast.makeText(FeedBackActivity.this,"提交失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }
    }

    private void initview(){
        fb_content= (EditText) findViewById(R.id.fb_content);
        fb_contact= (EditText) findViewById(R.id.fb_contact);
        svg_back= (ImageView) findViewById(R.id.svg_back);
        sub_fb= (Button) findViewById(R.id.sub_fb);
    }

    private void initEvent(){
        sub_fb.setOnClickListener(new subFB());
        svg_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FeedBackActivity.this,MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("page",4);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        });
    }

}
